665D - Simple Subset - CodeForces Solution


constructive algorithms greedy number theory *1800

Please click on ads to support us..

Python Code:

import sys
readline=sys.stdin.readline

class Prime:
    def __init__(self,N):
        assert N<=10**8
        self.smallest_prime_factor=[None]*(N+1)
        for i in range(2,N+1,2):
            self.smallest_prime_factor[i]=2
        n=int(N**.5)+1
        for p in range(3,n,2):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
                for i in range(p**2,N+1,2*p):
                    if self.smallest_prime_factor[i]==None:
                        self.smallest_prime_factor[i]=p
        for p in range(n,N+1):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
        self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]]

    def Factorize(self,N):
        assert N>=1
        factors=defaultdict(int)
        if N<=len(self.smallest_prime_factor)-1:
            while N!=1:
                factors[self.smallest_prime_factor[N]]+=1
                N//=self.smallest_prime_factor[N]
        else:
            for p in self.primes:
                while N%p==0:
                    N//=p
                    factors[p]+=1
                if N<p*p:
                    if N!=1:
                        factors[N]+=1
                    break
                if N<=len(self.smallest_prime_factor)-1:
                    while N!=1:
                        factors[self.smallest_prime_factor[N]]+=1
                        N//=self.smallest_prime_factor[N]
                    break
            else:
                if N!=1:
                    factors[N]+=1
        return factors

    def Divisors(self,N):
        assert N>0
        divisors=[1]
        for p,e in self.Factorize(N).items():
            pow_p=[1]
            for _ in range(e):
                pow_p.append(pow_p[-1]*p)
            divisors=[i*j for i in divisors for j in pow_p]
        return divisors

    def Is_Prime(self,N):
        return N==self.smallest_prime_factor[N]

    def Totient(self,N):
        for p in self.Factorize(N).keys():
            N*=p-1
            N//=p
        return N

    def Mebius(self,N):
        fact=self.Factorize(N)
        for e in fact.values():
            if e>=2:
                return 0
        else:
            if len(fact)%2==0:
                return 1
            else:
                return -1

N=int(readline())
A=list(map(int,readline().split()))
Pr=Prime(2*10**6)
cnt=A.count(1)
for a in A:
    if a==1:
        continue
    if Pr.Is_Prime(a+1) and cnt:
        ans=cnt+1
        ans_lst=[1]*cnt+[a]
        break
else:
    if cnt>=2:
        ans=cnt
        ans_lst=[1]*cnt
    else:
        for i in range(N):
            for j in range(i+1,N):
                if Pr.Is_Prime(A[i]+A[j]):
                    ans=2
                    ans_lst=[A[i],A[j]]
                    break
            else:
                continue
            break
        else:
            ans=1
            ans_lst=[A[0]]
print(ans)
print(*ans_lst)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using PII=pair<int,int>;

const int N=1e6+10;
int n;
int a[1010],t[N];
int cnt,num2,idx;
bool check(int x){
	if(x==2) return 1;
	for(int i=2,j=sqrt(x);i<=j;i++)
		if(x%i==0) return 0;
	return 1;
}
void tong(){
	for(int i=1;i<=N;i++){
		if(t[i]) a[++idx]=i;
	}
}
void solve(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		if(a[i]==1) cnt++;
		if(check(a[i]+1) && a[i]!=1) num2=a[i];
		t[a[i]]++;
	}
	if(cnt==1 && num2){cout << 2 << endl << 1 << " " << num2; return ;}
	if(cnt>=2){
		if(num2){
			cout << cnt+1 << endl;
			while(cnt--) cout << 1 << " ";
			cout << num2;
		}else{
			cout << cnt << endl;
			while(cnt--) cout << 1 << " ";
		}
		return ;
	}
	tong();
	for(int i=1;i<=idx;i++){
		for(int j=1;j<=idx;j++){
			if(i==j) continue;
			if(check(a[i]+a[j])){cout << 2 << endl << a[i] << " " << a[j]; return ;}
		}
	}
	cout << 1 << endl << a[1] << endl;
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t=1;
	//cin >> t;
	while(t--) solve();
	return 0;
}
//make it count
//开ll了没


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST